package com.badu.app.web;

import javax.validation.constraints.NotBlank;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.badu.app.domain.AppBanner;
import com.badu.app.domain.AppCoupon;
import com.badu.app.service.IAppCouponService;
import com.badu.common.controller.BaseController;
import com.badu.common.utils.R;
import com.badu.common.utils.StringUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder123
 * @since 2021-01-14
 */
@Validated
@Controller
@RequestMapping("/web/app/coupon")
public class AppCouponController extends BaseController{

	@Autowired
	private IAppCouponService couponService;
	
	@PostMapping("/addCoupon")
	public R addCoupon(AppCoupon coupon) {
		if(StringUtils.isBlank(coupon.getCouponType()))
			return R.error(" 缺少必填参数  优惠券类型");
		if(coupon.getDiscountMoney()==null)
			return R.error(" 缺少必填参数  优惠优惠金额");
		if(coupon.getUserMoney()==null)
			return R.error(" 缺少必填参数  优惠使用条件金额");
		if(coupon.getSendNum()==null)
			return R.error(" 缺少必填参数  优惠发行数量");
		if(StringUtils.isBlank(coupon.getCouponName()))
			return R.error(" 缺少必填参数  优惠券名称");
		if(coupon.getStartTime()==null)
			return R.error(" 缺少必填参数  优惠生效开始时间");
		if(coupon.getEndTime()==null)
			return R.error(" 缺少必填参数  优惠生效结束时间");
		
		coupon.setCreateUser(getUsername());
		return R.insertOrUpdateHandleResult(couponService.insert(coupon));
	}
	
	
	@PostMapping("updateCoupon")
	public R updateCoupon(AppCoupon coupon) {
		if(StringUtils.isBlank(coupon.getId()))
			return R.error(" 缺少必填参数  id");
		return R.insertOrUpdateHandleResult(couponService.updateById(coupon));
	}
	
	@PostMapping("deleteCoupon")
	public R deleteCoupon(@NotBlank(message = "id必填")String id) {
		return R.insertOrUpdateHandleResult(couponService.deleteById(id));
	}
	
	@GetMapping("queryCoupon")
	public R queryCoupon(@RequestParam(name = "page",required = false,defaultValue = "1")Integer page
			            ,@RequestParam(name = "size",required = false,defaultValue = "10")Integer size
			            ,AppCoupon coupon) {
		Page<AppCoupon> selectPage = couponService.selectPage(new Page<AppCoupon>(page, size),new EntityWrapper<AppCoupon>(coupon));
		return R.ok().put("data", selectPage);
	}
}

